package org.session.dao.impl;

import javax.ejb.Stateless;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.session.dao.PatientDao;
import org.session.dao.StudyDao;
import org.entity.Patient;
import org.entity.Study;

/**
 * Session Bean implementation class StudyDaoImpl
 */
@SuppressWarnings("unused")
@Stateless
@Local(StudyDao.class)
@Remote(StudyDao.class)
public class StudyDaoImpl implements StudyDao {

	@PersistenceContext
	private EntityManager em;
    /**
     * Default constructor. 
     */
    public StudyDaoImpl() {
        // TODO Auto-generated constructor stub
    }
    
    @SuppressWarnings("unchecked")
	public List<Study> findAllStudy() {
		return (List<Study>) em.createQuery("select stu from Study stu").getResultList();
    }
    
    public Study findStudyByPk(Integer pk){
    	return em.find(Study.class, pk);
    }
	
	public List<Study> findStudyByPatientPk(Integer pk){
		Query query = em.createQuery("select stu from Study stu where stu.patient.pk =:pk");
		query.setParameter("pk", pk);
		List<Study> stu = query.getResultList();
		return stu;
	}
	
	public void update(Study study){
		em.merge(study);
	}
}
